package com.withoutwords.engine;

import java.io.Serializable;
import java.security.acl.Owner;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.withoutwords.engine.social.SocialUser;

public class Game implements ConvertableJson, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6205809671200407381L;
	String gameId = "";

	public String getGameId() {
		return gameId;
	}

	ArrayList<SocialUser> gamePlayers;
	Round currentRound;
	SocialUser gameOwner = new SocialUser();
	boolean privateGame;

	int nRoundCount = 0;

	public int getnRoundCount() {
		return nRoundCount;
	}

	public void incrementRoundCount() {
		this.nRoundCount++;
	}

	public Round getCurrentRound() {
		return currentRound;
	}

	public void setCurrentRound(Round currentRound) {
		this.currentRound = currentRound;
		// incrementRoundCount();
	}

	public String getPlayerName(String playerUID) {
		String PlayerName = "";
		if(gamePlayers != null)
		{
		for (int i = 0; i < gamePlayers.size(); i++) {
			SocialUser player = gamePlayers.get(i);
			if (player.getSocialID().equalsIgnoreCase(playerUID)) {
				PlayerName = player.getName();
				break;
			}

		}
		}
		return PlayerName;
	}

	public boolean isPrivateGame() {
		return privateGame;
	}

	public void setPrivateGame(boolean privateGame) {
		this.privateGame = privateGame;
	}

	public void setGameOwner(SocialUser gameOwner) {
		if (gameOwner != null) {
			this.gameOwner = gameOwner;
		}
	}

	public void addPlayer(SocialUser player) {
		if (player != null) {
			if (gamePlayers == null) {
				gamePlayers = new ArrayList<SocialUser>();
			}
			gamePlayers.add(player);
		}
	}

	public void clearPlayerList() {
		if (gamePlayers != null) {
			gamePlayers.clear();
		}
	}

	public int getPlayersCount() {
		if (gamePlayers != null) {
			return gamePlayers.size();
		} else
			return 0;
	}

	public SocialUser getPlayer(int index) {
		try {
			return gamePlayers.get(index);
		} catch (IndexOutOfBoundsException e) {
			return null;
		}
	}
	public SocialUser getPlayerExecludingOwner(int index) {
		try {
			ArrayList<SocialUser> execludingList = (ArrayList<SocialUser>) gamePlayers.clone();
			execludingList.remove(this.gameOwner);
			return execludingList.get(index);
		} catch (IndexOutOfBoundsException e) {
			return null;
		}
	}

	public SocialUser getGameOwner() {
		return gameOwner;
	}

	@Override
	public JSONObject toJSON() {
		JSONObject obj = new JSONObject();
		try {
			obj.put(Common.gameIdCode, gameId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (gamePlayers != null) {
				JSONArray arrayJson = new JSONArray();
				for (int i = 0; i < gamePlayers.size(); i++) {
					arrayJson.put(i, gamePlayers.get(i).toJSON());
				}
				obj.put("players", arrayJson);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			int privInt = 0;
			if (privateGame) {
				privInt = 1;
			}
			obj.put("private", privInt);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (currentRound != null) {
				obj.put("current_round", currentRound.toJSON());
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			obj.put("owner", gameOwner.toJSON());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return obj;
	}

	@Override
	public boolean fromJSON(JSONObject obj) {
		try {
			gameId = obj.getString(Common.gameIdCode);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			privateGame = (obj.getInt("private") == 1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			currentRound = new Round();
			currentRound.fromJSON(obj.getJSONObject("current_round"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			//
			JSONArray jsonPlayers = obj.getJSONArray("players");
			gamePlayers = new ArrayList<SocialUser>();
			for (int i = 0; i < jsonPlayers.length(); i++) {
				SocialUser friend = new SocialUser();
				friend.fromJSON(jsonPlayers.getJSONObject(i));
				gamePlayers.add(friend);

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			gameOwner = new SocialUser();
			gameOwner.fromJSON(obj.getJSONObject("owner"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return false;
		}
		return true;

	}
	@Override
	public boolean equals(Object o) {
		Game other = (Game)o;
		return gameId.equals(other.gameId);
	}

	@Override
	public String toString() {
		SocialUser owner = getGameOwner();
		if (owner != null) {
			return owner.getName();
		} else
			return "";
	}

}
